home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
137_01
/
feb84col.ddj
< prev
next >
Wrap
Text File
|
1980-01-01
|
20KB
|
535 lines
.pl 60
.po 0
..
.. "The C/Unix Programmer's Notebook"
..
.. DDJ Column #2 as of 19-Sep-83
.. (C) 1983 Anthony Skjellum. All rights reserved.
.. For publication in DDJ
..
..
.. file (DDJCOL2.WS) created: 24-Nov-83
.. updated: 24-Nov-83
.. updated: 25-Nov-83
..
.. completed: 25-Nov-83
..
.he "C/Unix Programmer's Notebook" by Anthony Skjellum. (C) 1983. For 2/84 DDJ.
Introduction
..
In the first installment of this column, I proposed a standard for
the layout of C code (see DDJ #84, October, 1983.) Significant reader
response was received concerning this subject. The vast majority of the
letters received were in favor of the concept of a C layout standard. In
this column, I will present reader comments concerning the layout standard,
and discuss some modifications and additions to the proposed standard based
on reader suggestions.
..
Some comments were also received concerning my discussion of
runtime library and linkage format incompatibilities. Discussion of these
points will be left for a future column.
Modifying the Proposed Layout Standard
I. Questions of White Space
..
Several readers took exception to a particular point concerning the
proposed layout standard. This was point 4f. which states "No white space
character is placed between a keyword (e.g. if) and its parenthesized
argument." David D. Clark of State College, Pennsylvania, writes:
..
"In general, I like your coding standard suggestions. My only
strong objection is your idea to leave out spaces between reserved words
[and their arguments]. It makes them look like function invocations."
Tim Smith of Evanston, Illinois, notes:
..
"...I think that a single space between a function name and the
initial opening parenthesis, or after "if's" and "elses", looks better..."
Guy Scharf of Mountainview, California writes:
..
"4f. I have a strong preference to always put a space between a
reserved word (e.g. if, while) and its parenthesized argument. This adds
legibility for me."
Finally, Charlie Brady of New South Wales, Australia, writes:
..
"The only real beef I have with you is the formatting of keywords
and their parenthetical expressions. I can see no reason to depart from
Kernighan and Ritchie on this point, and a number of reasons for
maintaining their convention. Firstly, a flow control construct is
semantically distinct from a function call, and a formatting difference is
a reasonable way of distinguishing them. Secondly, the formatting
difference simplifies the use of a text editor for such tasks as
constructing structure charts. Third, your recommendation departs from at
least three extant recommended standards, namely Kernighan and Ritchie,
Thomas Plum (C Standards and Guidelines, Plum-Hall 1981) and Tim Lang
(Formatting C, AUUGN Vol 4, No 1, Jan 1982. Enclosed)." (I want to thank
Mr. Brady for including a copy of the Lang article with his letter. The C
standard proposed there is very compatible with the one I have proposed.)
..
After considering the above remarks, I have come to the conclusion
that the space really does serve a useful purpose. Therefore, I suggest
that point 4f. should be changed to read: "A single white character is
(optionally) placed between a keyword (e.g. if) and its parenthesized
argument." (Making the white space character optional is another point for
debate.) I think it should be optional, but recommended. I don't think
that adding a space for function call invocations would be beneficial, as
suggested by Mr. Smith.
..
Another question concerning white space insertion comes in
connection with argument lists. The original standard does not indicate if
spaces should be included. It is my opinion that a comma should be
directly adjacent to the argument that it follows, and that a single white
space should follow each comma to add legibility. I am also convinced that
parentheses should be adjacent to the argument(s) they enclose. Thus (in
agreement with Tim Lang's article mentioned above) I would write:
x = atan(sin(y));
and not
x = atan( sin( y ) );
..
Yet another point not previously mentioned is that binary operators
should be delimited by white space. Thus, the following statement lacks
sufficient white space:
v = sin(ln(1.0+x));
while this expression is properly formed:
v = sin(ln(1.0 + x));
..
Finally, section 4 needs to be updated to include a style
specification for pointer references. I think that that operators '.' and
'->' should not be delimited by spaces from the objects which they act on.
This point and the three above are formalized in Figure I. as additions to
section 4.
.pa
------------------FIGURE I.--------------------
(Under section 4)
b. Binary operators (e.g. +, -, / but not '->' and '.') and
assignment operators (eg =, *=, &=) are delimited by white
space.
g. Parentheses should be adjacent to the argument(s) which
they enclose.
h. A comma is bound to the argument which preceeds and should
be followed by a single space.
i. Operators such as '->' and '.' (used in pointer
references) directly bind to their arguments with no
intervening spaces.
---------------------END FIGURE I.--------------------
..
The Lang article points out a circumstance under which point 9g.
need not be followed. This occurs when very complicated conditional
expressions of the form "keyword (expr)" are split over several lines. For
example, instead of using a crowded expression as illustrated in Example
1a., a more readable form is selected (see Example 1b.) Note that in
Example 1b. that the parentheses are placed on lines by themselves, since
they bracket a multiline expression, much like braces enclose the
statements of a block.
--------------------Example Ia.---------------------
if((a == 1) && (b == 2) && (c == 3) && ((d == 4) || (d == 5)))
{
/* operations performed if conditional true */
...
}
--------------------End Example Ia.--------------------
--------------------Example Ib.---------------------
if /* we make multi-line expression look like a block */
(
(a == 1) &&
(b == 2) &&
(c == 3) &&
((d == 4) || (d == 5))
)
{
/* operations performed if conditional true */
...
}
--------------------End Example Ib.--------------------
..
Another point of minor objection was the tabulation method
specified by the standard (point 1a.). Steve Newberry of Los Altos,
California, states:
..
"Upon one point I do feel compelled to argue with you, and that is
the tab convention: The DEPTH of the tab stop on a given page is of far
less significance to the readability of that page than is the CONSISTENCY
of the depth. I REALLY don't want to use different size tabs on the same
page."
Tim Smith writes:
"I personally follow most all of his suggestions on how to actually
format the code on the line and page, with only two exceptions. I ALWAYS
use 4-space tab stops..."
..
I agree that having a single tab size is the preferable way to
write C code. Standard tabs give more openness to the code, and make
various parts of a program easier to pick out. My rationale for large
horizontal tabbing